<?php
class bootstrap {
    
    protected $registry;

    public function __construct($registry) {
		$this->registry = $registry;
	}
	
	public function __get($key) {
		return $this->registry->get($key);
	}
	
	public function __set($key, $value) {
		$this->registry->set($key, $value);
	}

    public function bootstrap() {

        // Response
        $this->registry->set('response', new Response());

        // Request
        $this->registry->set('request', new Request());

        // Document
        $this->registry->set('document', new Document());	

        // Url
        $this->registry->set('url', new Url());

        // Cache        
        $dir_cache = $this->load->get_path('dir_cache');
        $this->registry->set('cache', new Cache($dir_cache)); 
        unset($dir_cache);

        // Session
        $this->registry->set('session', new Session());

        // Setting
        $this->registry->set('setting', new Registry());

        // Database  
        $this->cfg_system->set_section('database');

        $this->registry->set('pdo' , new database(
            $this->cfg_system->get('db_driver'),
            $this->cfg_system->get('db_host'),
            $this->cfg_system->get('db_user'),
            $this->cfg_system->get('db_password'),
            $this->cfg_system->get('db_name')
        ));

        $this->setting->set('db_prefix', $this->cfg_system->get('db_prefix'));

        // Settings Config
        $setting = new setting($this->registry);
        $this->setting->merge($setting->load(0));
        unset($setting);
        
        // Auth 
        $auth = new auth(
            $this->registry->get('request'),
            $this->registry->get('session'),
            new user_dao($this->registry)
        );

        // User
        if($auth->is_logged() && $auth->validate_token()){
            $this->registry->set('user', $auth->get_user());
        }

        $this->registry->set('auth', $auth); 

        $this->response->addHeader('Content-Type: text/html; charset=UTF-8');

        // Application Classes
        $this->registry->get("load")->library("cart");

        // Currency
        $this->registry->set('currency', new Currency($this->registry));		

        // Weight
        $this->registry->set('weight', new Weight($this->registry));

        // Length
        $this->registry->set('length', new Length($this->registry));

        // Old Database 
        $this->registry->set('db', new DB(
            $this->cfg_system->get('db_driver'),
            $this->cfg_system->get('db_host'),
            $this->cfg_system->get('db_user'),
            $this->cfg_system->get('db_password'),
            $this->cfg_system->get('db_name')
        ));

    }
}
